package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    //返回列表
    @Override
    public List<Category> findAll() {

        return categoryMapper.selectList(null);
    }
    //添加
    @Override
    public void save(Category category) {
        category.setId(IdUtil.getSnowflake(1,1).nextId());
        categoryMapper.save(category);
    }

    //修改
    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
    }
    //删除
    @Override
    public void delete(Long id) {
        if (categoryMapper.countDishByCategoryId(id) > 0) {
            throw new CustomException("该分类下有菜品，不允许删除");
        }
        if (categoryMapper.countSetmealByCategoryId(id) > 0) {
            throw new CustomException("该分类下有套餐，不允许删除");
        }
        categoryMapper.deleteById(id);

    }
    //回显分类
    @Override
    public List<Category> findByType(Integer type) {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType, type);
        qw.orderByAsc(Category::getType);
        return categoryMapper.selectList(qw);
    }




}
